#include "City.h"

City::City()
{
	srand(unsigned(time(NULL)));
	m_immigrants = 5;
	m_currStarved = 0;
	m_currentBushels = 2800;
	m_grainEaten = 200;
	m_costPerAcre = (rand() % 10) + 17;
	m_harvested = 0;
	m_plague = false;

	m_totalStarved = 0;
	m_population = 100;
	m_acre = 1000;
	m_totalBushels = 2800;
	m_year = 1;

	m_percentPopDied = 0;
	m_acresPrePerson = 0;
	m_rulerPerformance = 0;

	m_tempPurchase = 0 ;
	m_tempFeed = 0 ;
	m_tempPlant = 0 ;

	msg = " ";

}

City::~City()
{
}

void City::UpdateCity(bool a_plag, int a_imm, int a_starved, int a_grainEaten, int a_grainHarvested, int a_acre, int a_planted)
{
	if(a_plag)
	{
		m_plague = a_plag;
		m_population /= 2; //cuts population when plague hits
	}
	m_immigrants = a_imm;
	m_population += a_imm;
	m_currStarved = a_starved;
	m_totalStarved += a_starved;
	m_grainEaten = a_grainEaten;
	m_harvested = a_grainHarvested;
	m_population -= m_currStarved;

	m_totalBushels = m_currentBushels ;

	m_totalBushels -= m_grainEaten ;
	m_totalBushels += a_grainHarvested * a_planted;
	m_currentBushels = m_totalBushels;

	m_acre += a_acre;
	m_costPerAcre = (rand() % 10) + 17;
	m_year++ ;

	m_percentPopDied=((m_year - 1)* m_percentPopDied + (m_totalStarved * 100 / m_population))/ m_year;

	m_tempPurchase = 0 ;
	m_tempFeed = 0 ;
	m_tempPlant = 0 ;


}

string City::Display()
{
	//clear screen
	//system("CLS");

	strcpy(txt, "Hammurabi: I beg to report to you,\n\r");

	strcpy(txt+strlen(txt), "In Year ");
	strcpy(txt+strlen(txt), itoa(m_year, num, sizeof(num)));
	strcpy(txt+strlen(txt), ", ");
	strcpy(txt+strlen(txt), itoa(m_currStarved, num, sizeof(num)));
	strcpy(txt+strlen(txt), " people starved.\n\r");

	strcpy(txt+strlen(txt), itoa(m_immigrants, num, sizeof(num)));
	strcpy(txt+strlen(txt), " people came to the city.\n\r");

	if(m_plague)
	{
		strcpy(txt+strlen(txt), "A horrible plague struck! Half the population died.\n\r");
		m_plague = false;
	}
	strcpy(txt+strlen(txt), "The city population is now ");
	strcpy(txt+strlen(txt), itoa(m_population, num, sizeof(num)));

	strcpy(txt+strlen(txt), "\n\rThe city now owns ");
	strcpy(txt+strlen(txt), itoa(m_acre, num, sizeof(num)));
	strcpy(txt+strlen(txt), " acres.\n\r");

	strcpy(txt+strlen(txt), "You harvested ");
	strcpy(txt+strlen(txt), itoa(m_harvested, num, sizeof(num)));
	strcpy(txt+strlen(txt), " bushels per acre.\n\r");

	strcpy(txt+strlen(txt), "Rats ate ");
	strcpy(txt+strlen(txt), itoa(m_grainEaten, num, sizeof(num)));
	strcpy(txt+strlen(txt), " bushels.\n\r");

	strcpy(txt+strlen(txt), "You now have ");
	strcpy(txt+strlen(txt), itoa(m_totalBushels, num, sizeof(num)));
	strcpy(txt+strlen(txt), " bushels in store.\n\r");

	strcpy(txt+strlen(txt), "Land is trading at ");
	strcpy(txt+strlen(txt), itoa(m_costPerAcre, num, sizeof(num)));
	strcpy(txt+strlen(txt), " bushels per acre.\n\r");

	strcpy(txt+strlen(txt), "\nYou have ");
	strcpy(txt+strlen(txt), itoa(m_currentBushels, num, sizeof(num)));
	strcpy(txt+strlen(txt), " bushels available.\n\n\r");

	//display text
	//cout << "Hammurabi: I beg to report to you," << endl;
	//cout << "In Year " << m_year << ", " << m_currStarved << " people starved." << endl;
	//cout << m_immigrants << " people came to the city." << endl;
	//cout << "The city population is now " << m_population << endl;
	//cout << "The city now owns " << m_acre << " acres." << endl;
	//cout << "You harvested " << m_harvested << " bushels per acre." << endl;
	//cout << "Rats ate " << m_grainEaten << " bushels." << endl;
	//cout << "You now have " << m_totalBushels << " bushels in store." << endl;
	//cout << "Land is trading at " << m_costPerAcre << " bushels per acre." << endl;

	//cout << "\nYou have " << m_currentBushels << " bushels available." << endl;

	msg = txt;
	return msg;
}

bool City::GetValidation(int input, int option)
{
	switch(option)
	{
	case PURCHASE:	//validates purchases
		{
			if(input > 0)
			{
				//BUYING
				//checks if input (amount to purchase) is more than the current bushels
				if((input * m_costPerAcre) > m_currentBushels)
				{
					//cout << "You do not have enough bushels to complete this action." << endl;
					//cout << "Current bushels owned: " << m_currentBushels << endl;
					return false;
				}
				else
				{ 
					if( input != m_tempPurchase )
					{
						m_currentBushels -= (input - m_tempPurchase) * m_costPerAcre ;
						m_tempPurchase = input ;
					}

					return true;	
				}
			}
			else
			{
				//SELLING
				//checks if input is more than the acres owned
				if(input < -m_acre)
				{
					return false;
				}
				else
				{ 
					if( input != m_tempPurchase )
					{
						m_currentBushels -= (input - m_tempPurchase) * m_costPerAcre ;
						m_tempPurchase = input ;
					}
					return true;	
				}
			}
		}break;
	case FEED:	//validates bushels to feed
		{
			if(input > m_currentBushels || input < 0)
			{
				return false;
			}
			else
			{ 
				if( input != m_tempFeed )
					{
						m_currentBushels -= (input - m_tempFeed) ;
						m_tempFeed = input ;
					}
				return true;	
			}
		}break;
	case PLANT:	//validates bushels to plant
		{
			if(input > m_acre || input < 0)
			{
				return false;
			}
			else if(input > m_currentBushels)
			{
				return false;
			}
			else if (input > m_population * 10)
			{
				return false;
			}
			else
			{ 
				if( input != m_tempPlant )
					{
						m_currentBushels -= (input - m_tempPlant) ;
						m_tempPlant = input ;
					}
				return true;	
			}

		}break;
	}
}

string City::DisplayEnd()
{
	//clear screen
	//system("CLS");

	strcpy(txt, "IN YOUR 10-YEAR TERM OF OFFICE, ");
	strcpy(txt+strlen(txt), itoa(m_percentPopDied, num, sizeof(num)));
	strcpy(txt+strlen(txt), " PERCENT OF THE\n\r");
	strcpy(txt+strlen(txt), "POPULATION STARVED PER YEAR ON AVERAGE, I.E., A TOTAL OF\n\r");
	strcpy(txt+strlen(txt), itoa(m_totalStarved, num, sizeof(num)));
	strcpy(txt+strlen(txt), " PEOPLE DIED\n\r");
	m_acresPrePerson = m_acre/m_population;
	strcpy(txt+strlen(txt), "YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH\n\r");
	strcpy(txt+strlen(txt), itoa(m_acresPrePerson, num, sizeof(num)));
	strcpy(txt+strlen(txt), " ACRES PER PERSON\n\n\n\r");

	if ((m_percentPopDied>33) || (m_acresPrePerson <7)) m_rulerPerformance = 1;
	else if ((m_percentPopDied>10) || (m_acresPrePerson <9)) m_rulerPerformance = 2;
	else if ((m_percentPopDied>3) || (m_acresPrePerson <10)) m_rulerPerformance = 3;
	else m_rulerPerformance = 4;

	//******taken from HammurabiNcpp.txt provided by Prof.
	//860 PRINT "IN YOUR 10-YEAR TERM OF OFFICE,"percentPopDied"PERCENT OF THE"
	//cout << "IN YOUR 10-YEAR TERM OF OFFICE, " << m_percentPopDied << " PERCENT OF THE" << endl;
	//862 PRINT "POPULATION STARVED PER YEAR ON AVERAGE, I.E., A TOTAL OF"
	//cout << "POPULATION STARVED PER YEAR ON AVERAGE, I.E., A TOTAL OF" << endl;
	//865 PRINT totalDeaths"PEOPLE DIED!!":
	//cout << m_totalDeaths << " PEOPLE DIED!!" << endl;
	// acresOwnedPerPerson=acresOwned/population
	//m_acresPrePerson = m_acre/m_population;
	//870 PRINT "YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH"
	//cout << "YOU STARTED WITH 10 ACRES PER PERSON AND ENDED WITH" << endl;
	//875 PRINT acresOwnedPerPerson "ACRES PER PERSON.":
	//cout << m_acresPrePerson << " ACRES PER PERSON." << endl;
	//880 IF percentPopDied>33 THEN 565 
	//885 IF acresOwnedPerPerson <7 THEN 565
	//if ((m_percentPopDied>33) || (m_acresPrePerson <7)) m_rulerPerformance = 1;
	//890 IF percentPopDied>10 THEN 940
	//892 IF acresOwnedPerPerson <9 THEN 940
	//else if ((m_percentPopDied>10) || (m_acresPrePerson <9)) m_rulerPerformance = 2;
	//895 IF percentPopDied>3 THEN 960
	//896 IF acresOwnedPerPerson <10 THEN 960
	//else if ((m_percentPopDied>3) || (m_acresPrePerson <10)) m_rulerPerformance = 3;
	//else m_rulerPerformance = 4;

	// PRINT
	//cout << endl;
	switch (m_rulerPerformance)
	{
	case 1:

		strcpy(txt+strlen(txt), "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY\n\r");
		strcpy(txt+strlen(txt), "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE\n\r");
		strcpy(txt+strlen(txt), "ALSO BEEN DECLARED 'NATIONAL FINK' !!\n\r");

		//565 PRINT "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY"
		//cout << "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY" << endl;
		//566 PRINT "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE"
		//cout << "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE" << endl;
		//567 PRINT "ALSO BEEN DECLARED 'NATIONAL FINK' !!":
		//cout << "ALSO BEEN DECLARED 'NATIONAL FINK' !!" << endl;
		// GOTO 990
		break;
	case 2:

		strcpy(txt+strlen(txt), "YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV.\n\r");
		strcpy(txt+strlen(txt), "THE PEOPLE (REMAINING) FIND YOU AN UNPLEASANT RULER, AND,\n\r");
		strcpy(txt+strlen(txt), "FRANKLY, HATE YOUR GUTS!\n\r");

		//940 PRINT "YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV."
		//cout << "YOUR HEAVY-HANDED PERFORMANCE SMACKS OF NERO AND IVAN IV." << endl;
		//945 PRINT "THE PEOPLE (REMAINING) FIND YOU AN UNPLEASANT RULER, AND,"
		//cout << "THE PEOPLE (REMAINING) FIND YOU AN UNPLEASANT RULER, AND," << endl;
		//950 PRINT "FRANKLY, HATE YOUR GUTS!":
		//cout << "FRANKLY, HATE YOUR GUTS!" << endl;
		// GOTO 990
		break;
	case 3:
		strcpy(txt+strlen(txt), "YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT\n\r");
		strcpy(txt+strlen(txt), "REALLY WASN'T TOO BAD AT ALL. \n\r");
		strcpy(txt+strlen(txt), itoa(int(float(m_population)*(float(rand() % 8)/10.0)), num, sizeof(num)));

		strcpy(txt+strlen(txt), " PEOPLE WOULD\n\r");
		strcpy(txt+strlen(txt), "DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR\n\r");
		strcpy(txt+strlen(txt), "TRIVIAL PROBLEMS.\n\r");

		////960 PRINT "YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT"
		//cout << "YOUR PERFORMANCE COULD HAVE BEEN SOMEWHAT BETTER, BUT" << endl;
		////965 PRINT "REALLY WASN'T TOO BAD AT ALL. ";
		//cout << "REALLY WASN'T TOO BAD AT ALL. " << endl;
		////966 PRINT INT(population*.8*RND(1));"PEOPLE WOULD"
		//cout << int(float(m_population)*(float(rand() % 8)/10.0)) << " PEOPLE WOULD" << endl;
		////970 PRINT "DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR"
		//cout << "DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR" << endl;
		////975 PRINT "TRIVIAL PROBLEMS."
		//cout << "TRIVIAL PROBLEMS." << endl;
		break;
	case 4:
		strcpy(txt+strlen(txt), "A FANTASTIC PERFORMANCE!!!  CHARLEMANGE, DISRAELI, AND\n\r");
		strcpy(txt+strlen(txt), "JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!\n\r");

		//900 PRINT "A FANTASTIC PERFORMANCE!!!  CHARLEMANGE, DISRAELI, AND"
		//cout << "A FANTASTIC PERFORMANCE!!!  CHARLEMANGE, DISRAELI, AND" << endl;
		//905 PRINT "JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!":
		//cout << "JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!" << endl;
		// GOTO 990
		break;
	}
	// *****************************Shutdown*******************************************
	//990 PRINT:
	// FOR N=1 TO 10:
	// PRINT CHR$(7);:
	// NEXT N
	strcpy(txt+strlen(txt), "\n\nSO LONG FOR NOW.\n\r");

	//995 PRINT "SO LONG FOR NOW.":
	//cout << endl << "SO LONG FOR NOW." << endl;

	msg = txt;
	return msg;
}

string City::DisplayLose()
{
	strcpy(txt,  "YOU STARVED ");
	strcpy(txt+strlen(txt), itoa(m_currStarved, num, sizeof(num)));
	strcpy(txt+strlen(txt), " PEOPLE IN ONE YEAR!!!\n\n\r");

	strcpy(txt+strlen(txt), "DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY\n\r");
	strcpy(txt+strlen(txt), "BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE\n\r");
	strcpy(txt+strlen(txt), "ALSO BEEN DECLARED 'NATIONAL FINK' !!\n\r");

	msg = txt;
	return msg;
}
